clear all
set matsize 11000

* Housekeeping
cd "/replication/"
use "data/long_data", clear

* keep matched towns only
drop if jurisdiction=="NA"
drop if jurisdiction==""
drop if lat==0
by townID, sort: gen nobs=[_N]
drop if nobs!=2

* Create and re-code variables
gen royal = 0
replace royal = 100 if jurisdiction=="Royal Jurisdiction"
replace royal=. if jurisdiction=="NA"

gen seigniorial = 0
replace seigniorial=1 if jurisdiction=="Seigneurial Jurisdiction"
replace seigniorial=. if jurisdiction=="NA"

gen ecclesiastical = 0
replace ecclesiastical=1 if jurisdiction=="Ecclesiastical Jurisdiction"
replace ecclesiastical=. if jurisdiction=="NA"

encode jurisdiction, gen(code_jur)
replace code_jur=. if year==1787

bysort townID: egen initial = mean(code_jur)

gen exposure_conflict = (weighted_conflict_300years-.1356093)/0.0305889


* post 1500 dummy
gen post1500 = 0
replace post1500=1 if y2==1800

* make interaction
gen proximity_coast=-distance_coast
gen atlanticXpost1500 = proximity_coast*post1500

* quadratic distance
gen proximity_sq = -(distance_coast^2)
gen atlantic_sqXpost1500 = proximity_sq*post1500

* log distance
gen proximity_log = -(log(distance_coast))
gen atlantic_logXpost1500 = proximity_log*post1500

* create quartiles of distance to Atlantic
gen quartiles = 0
replace quartiles = 1 if distance_coast<52.62
replace quartiles = 2 if distance_coast>52.61 & distance_coast<83.2
replace quartiles = 3 if distance_coast>83.2 & distance_coast<122.87

gen q1=0
replace q1=1 if quartiles==1

gen q2=0
replace q2=1 if quartiles==2

gen q3=0
replace q3=1 if quartiles==3

* interact them with post1500 dummy
gen q1Xpost = q1*post1500
gen q2Xpost = q2*post1500
gen q3Xpost = q3*post1500



* Set panel structure
xtset townID y2


*************************************
* DRIVERS OF ROYAL JURISDICTION
*************************************

* Table 2 (in the paper)

xtreg royal atlanticXpost1500 i.y2, fe robust
outreg2 using "out/table2A.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlanticXpost1500)

xtreg royal atlantic_sqXpost1500 i.y2, fe robust
outreg2 using "out/table2A.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlantic_sqXpost1500)

xtreg royal atlantic_logXpost1500 i.y2, fe robust
outreg2 using "out/table2A.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlantic_logXpost1500)

xtreg royal q1Xpost q2Xpost q3Xpost i.y2, fe robust
outreg2 using "out/table2A.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1Xpost q2Xpost q3Xpost)


**************************************************************

gen proximity_port = -(distance_port)
gen proximity_portsq = -(distance_port^2)
gen proximity_portlog = -(log(distance_port))


gen q1port = 0
gen q2port = 0
gen q3port = 0

replace q1port = 1 if distance_port<37.82
replace q2port = 1 if distance_port>37.82 & distance_port<60.82
replace q3port = 1 if distance_port>60.82 & distance_port<115.74

gen proximity_port_post = proximity_port*post1500
gen proximity_portsq_post = proximity_portsq*post1500
gen proximity_portlog_post = proximity_portlog*post1500

gen q1portpost = q1port*post1500
gen q2portpost = q2port*post1500
gen q3portpost = q3port*post1500



xtreg royal proximity_port_post i.y2, fe robust
outreg2 using "out/table2B.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_port_post)

xtreg royal proximity_portsq_post i.y2, fe robust
outreg2 using "out/table2B.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_portsq_post)

xtreg royal proximity_portlog_post i.y2, fe robust
outreg2 using "out/table2B.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_portlog_post)

xtreg royal q1portpost q2portpost q3portpost i.y2, fe robust
outreg2 using "out/table2B.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1portpost q2portpost q3portpost)

* binned scatterplots
binscatter royal proximity_port if year==1352
binscatter royal proximity_port if year==1787

**************************************************************

gen proximity_bilbao = -(distance_bilbao)
gen proximity_bilbaosq = -(distance_bilbao^2)
gen proximity_bilbaolog = -(log(distance_bilbao))


gen q1bilbao = 0
gen q2bilbao = 0
gen q3bilbao = 0

replace q1bilbao = 1 if distance_bilbao<85.44925
replace q2bilbao = 1 if distance_bilbao>85.44925 & distance_bilbao<122.1663 
replace q3bilbao = 1 if distance_bilbao>122.1663  & distance_bilbao<155.3353

gen proximity_bilbao_post = proximity_bilbao*post1500
gen proximity_bilbaosq_post = proximity_bilbaosq*post1500
gen proximity_bilbaolog_post = proximity_bilbaolog*post1500

gen q1bilbaopost = q1bilbao*post1500
gen q2bilbaopost = q2bilbao*post1500
gen q3bilbaopost = q3bilbao*post1500


xtreg royal proximity_bilbao_post i.y2, fe robust
outreg2 using "out/table2C.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbao_post)

xtreg royal proximity_bilbaosq_post  i.y2, fe robust
outreg2 using "out/table2C.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbaosq_post)

xtreg royal proximity_bilbaolog_post i.y2, fe robust
outreg2 using "out/table2C.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbaolog_post)

xtreg royal q1bilbaopost q2bilbaopost q3bilbaopost  i.y2, fe robust
outreg2 using "out/table2C.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1bilbaopost q2bilbaopost q3bilbaopost)


**************************************************************
* Statistical controls

xtreg royal atlanticXpost1500 exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsA.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlanticXpost1500 exposure_conflict UP)

xtreg royal atlantic_sqXpost1500 exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsA.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlantic_sqXpost1500 exposure_conflict UP)

xtreg royal atlantic_logXpost1500 exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsA.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(atlantic_logXpost1500 exposure_conflict UP)

xtreg royal q1Xpost q2Xpost q3Xpost exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsA.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1Xpost q2Xpost q3Xpost exposure_conflict UP)


xtreg royal proximity_port_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsB.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_port_post exposure_conflict UP)

xtreg royal proximity_portsq_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsB.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_portsq_post exposure_conflict UP)

xtreg royal proximity_portlog_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsB.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_portlog_post exposure_conflict UP)

xtreg royal q1portpost q2portpost q3portpost exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsB.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1portpost q2portpost q3portpost exposure_conflict UP)


xtreg royal proximity_bilbao_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsC.doc", replace nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbao_post exposure_conflict UP)

xtreg royal proximity_bilbaosq_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsC.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbaosq_post exposure_conflict UP)

xtreg royal proximity_bilbaolog_post exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsC.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(proximity_bilbaolog_post exposure_conflict UP)

xtreg royal q1bilbaopost q2bilbaopost q3bilbaopost exposure_conflict UP i.y2, fe robust
outreg2 using "out/controlsC.doc", append nocon tex dec(4) addtext(Town FE, Yes, Year FE, Yes) keep(q1bilbaopost q2bilbaopost q3bilbaopost exposure_conflict UP)


* Multinomial analysis

* 6 = seigniorial
* 5 = royal
* 4 = Mixed
* 3 = military order
* 2 = Behetria
* 1 = Ecclesiastical 

replace road = -road
replace river = -river

encode jurisdiction, gen(jur)
replace jur = 1 if jur==3
drop if jur==4
tab jur


mlogit jur proximity_coast road river if initial==1 & year==1787, base(1)

mlogit jur proximity_coast road river if initial==6 & year==1787, base(6)

mlogit jur proximity_coast road river if initial==5 & year==1787, base(5)

mlogit jur proximity_coast road river if initial==2 & year==1787, base(5)


mlogit jur proximity_port road river if initial==1 & year==1787, base(1)

mlogit jur proximity_port road river if initial==6 & year==1787, base(6)

mlogit jur proximity_port road river if initial==5 & year==1787, base(5)

mlogit jur proximity_port road river if initial==2 & year==1787, base(5)


mlogit jur proximity_bilbao road river if initial==1 & year==1787, base(1)

mlogit jur proximity_bilbao road river if initial==6 & year==1787, base(6)

mlogit jur proximity_bilbao road river if initial==5 & year==1787, base(5)

mlogit jur proximity_bilbao road river if initial==2 & year==1787, base(5)




